clear, close, clc

x = 0.1:0.1:3;
f = sin(x);
f = f + .01*randn(size(f));

plot(x,sin(x),'k')
hold on
plot(x,f,'rx')

dx = x(2) - x(1);
n = length(f);

dfdx = zeros(n,1);

dfdx(1) = (f(2)-f(1))/dx;
for i=2:n-1
    dfdx(i) = (f(i+1)-f(i-1))/(2*dx);
end
dfdx(n) = (f(n)-f(n-1))/dx;

figure
plot(x,cos(x),'k')
hold on
plot(x, dfdx, 'rx')